
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style type="text/css">
            .layui-table-cell {
                height: auto!important;
            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.tupay.tupayconfig.insert">
                <i class="layui-icon layui-icon-add-1"></i>新增
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.tupay.tupayconfig.delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <div>
                <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.tupay.tupayconfig.update">编辑</button>
                <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.tupay.tupayconfig.delete">删除</button>
            </div>
            <div>
                <button class="pear-btn pear-btn-xs tool-btn" lay-event="setConfig" permission="app.tupay.tupayconfig.setConfig">支付配置</button>
            </div>
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        <script src="/app/admin/admin/js/layuiAdmin.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/tuPay/tupay-config/select";
            const UPDATE_API = "/app/tuPay/tupay-config/update";
            const DELETE_API = "/app/tuPay/tupay-config/delete";
            const INSERT_URL = "/app/tuPay/tupay-config/insert";

            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util","soulTable"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					// },{
					// 	title: "主键",align: "center",
					// 	field: "id",

					},{
						title: "配置名",align: "center",
						field: "name",
						// templet: function (d) {
						//     return layuiTemplet(d,"name",apiResults)
						// }
					},{
						title: "支付方式",align: "center",
						field: "alias",
					},{
						title: "备注",align: "center",
						field: "remark",
					// },{
					// 	title: "配置项",align: "center",
					// 	field: "config",
					// 	hide: true,
					},{
						title: "状态",align: "center",
						field: "status",
						templet: function (d) {
						    return func.templetSwitch(d, "status");
						}
					},{
						title: "创建时间",align: "center",
						field: "created_at",
						sort: true,
                        hide: true,

					// },{
					// 	title: "更新时间",align: "center",
					// 	field: "updated_at",
					// 	hide: true,
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 120,
					}
				];
				
				// 获取表格自定义表头和列宽
				cols = layuiTableFilter(SELECT_API,cols)
				
				// 渲染表格
				function render()
				{
				    layuiRender({
				        url: SELECT_API,
				        cols:[cols],
				        // 表格拖拽回调  weight 是默认的排序字段 按需调整
				        // where:{field:'weight',type:'desc'},
				        // rowDrag:{done:(obj)=>{func.rowDragDoneFunc(obj,'weight')}},
				    });
				}
				
				
				// 获取表格中下拉或树形组件数据
				let apis = [];
				apis.push(["name", "/app/tuPay/index/payType"]);
				let apiResults = {};
				apiResults["name"] = [];
				let count = apis.length;
				layui.each(apis, function (k, item) {
				    let [field, url] = item;
				    $.ajax({
				        url: url,
				        dateType: "json",
				        success: function (res) {
				            if (res.code) {
				                return layui.popup.failure(res.msg);
				            }
				            function travel(items) {
				                for (let k in items) {
				                    let item = items[k];
				                    apiResults[field][item.value] = item.name;
				                    if (item.children) {
				                        travel(item.children);
				                    }
				                }
				            }
				            travel(res.data);
				        },
				        complete: function () {
				            if (--count === 0) {
				                render();
				            }
				        }
				    });
				});
				if (!count) {
				    render();
				}
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        func.remove(obj);
                    } else if (obj.event === "edit") {
                        func.edit(obj);
                    } else if(obj.event == 'setConfig'){
                        let value = obj.data[PRIMARY_KEY];
                        layerOpen('修改配置 - '+obj.data['alias'],"setConfig?id="+value+"&name="+obj.data['name'],'600px','80%')
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        func.add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        func.batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                layuiFormQuery('table-query');

                // 表格顶部搜索重置事件
                layuiFormReset('table-reset');

                // 表格排序事件
                layuiTableSort();

                // 新增 编辑 删除的方法集合
                 let func = lauiInit(PRIMARY_KEY,INSERT_URL,UPDATE_API,DELETE_API);
            })

        </script>
    </body>
</html>
